<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
    /**
     * 自定义验证规则
     */
    function validateForm() {
        form.bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                discountCouponName: {
                    validators: {
                        notEmpty: {message: "优惠劵名称不可为空"},
                        stringLength: {min:2,max: 50, message: "优惠劵名称长度为2至50"}
                    }
                },
                discountCouponAmount: {
                    validators: {
                        notEmpty: {message: "优惠金额不可为空"},
                        stringLength: {max: 10, message: "优惠金额最大长度为10"},
                        regexp: {
                            regexp: validateUtil.decimal2bit,
                            message: '只能保留二位小数'
                        },
                        numeric: {
                            message: '只能输入数字'
                        }
                    }
                },
                discountCouponLowAmount: {
                    validators: {
                        notEmpty: {message: "优惠券使用下限金额不可为空"},
                        stringLength: {max: 10, message: "优惠券使用下限金额最大长度为10"},
                        regexp: {
                            regexp: validateUtil.decimal2bit,
                            message: '只能保留二位小数'
                        },
                        numeric: {
                            message: '只能输入数字'
                        },
                        callback: {message: '优惠券使用下限金额不能小于优惠金额！',
                            callback: function(value, validator) {
                                var discountCouponAmount=$("#discountCouponAmount").val();
                                if(parseInt(value*100) < parseInt(discountCouponAmount*100)){
                                    return false;
                                }
                                return true;
                            }
                        }
                    }
                },
                distributionCount: {
                    validators: {
                        notEmpty: {message: "发放个数不可为空"},
                        stringLength: {max: 8, message: "发放个数最大长度为8"},
                        numeric: {
                            message: '只能输入数字'
                        }
                    }
                },
                beginTime: {
                    validators: {
                        notEmpty: {message: "开始时间不可为空"}
                    }
                },
                expirationTime: {
                    validators: {
                        notEmpty: {message: "结束时间不可为空"},
                        callback: {message: '结束时间应大于开始时间',
                            callback: function(value, validator) {
                                var beginTime=$("#beginTime").val();
                                if(new Date(value).valueOf() <= new Date(beginTime).valueOf()){
                                    return false;
								}
								return true;
                            }
                        }
                    }
                }
            }
        });
    }

    /**
     * 添加优惠券
     */
	<shiro:hasPermission name="back_gotoDiscountCouponPage_add">
    function addInfo() {
        $("#myModalLabel").empty().text("添加优惠券");
        /**清空隐藏域的值**/
        $("#discountCouponId").val("");
        $("#discountCouponCode").val("");
        $("#useStatus").val("");
        $("#isPass").val("");
        $("#checkTime").val("");
        $("#verifier").val("");
        $("#createTime").val("");
        $("#updateTime").val("");
        $("#surplus").val("");

        /**重置可见表单控件**/
        resetAddForm();
        /**初始化显示并控制时间选择器**/
        showStartDatePicker(new Date());
        showEndDatePicker(new Date());
    }
    </shiro:hasPermission>

    /**
	 * 1、控制开始时间和结束时间在表单中选择的逻辑
     * 2、开始时间小于结束时间并且精确到分钟可选择
     */
    function showStartDatePicker(startDate){
        $("#beginTime").datetimepicker({
            format: 'yyyy-mm-dd hh:ii', showMeridian: true, language: 'zh-CN', autoclose: true, todayBtn: true, startDate: startDate
        }).on('changeDate', function(event_start) {
            /**指定表单字段更新成功的验证状态**/
            updateValidateFieldStatus('beginTime');
            showEndDatePicker(event_start.date);
            $("#expirationTime").val("");
            reValidateField( 'expirationTime');
        });
    }

    /**
     * 1、通过开始时间控制结束时间的展示
     */
    function showEndDatePicker(selectedStartDate){
        $("#expirationTime").datetimepicker({
            format: 'yyyy-mm-dd hh:ii', showMeridian: true, language: 'zh-CN', autoclose: true, todayBtn: true, startDate: selectedStartDate
        }).on('changeDate', function(event_end) {
            if (event_end.date.valueOf() > selectedStartDate.valueOf()){
                /**指定表单字段更新成功的验证状态**/
                updateValidateFieldStatus('expirationTime');
            }
        });
    }

    /**
     * 1、处理时间选择器精确到分钟的表单提交
	 * 2、在分钟后进行秒:00的追加处理
     */
    function submitDiscountCouponForm() {
        /**1、对非空时间进行秒的处理**/
        var beginTime=$("#beginTime").val();
        var expirationTime=$("#expirationTime").val();
        if(beginTime!="" && expirationTime!=""){
            $("#beginTime").val(beginTime.substring(0,16)+":00");
            $("#expirationTime").val(expirationTime.substring(0,16)+":00");
        }
        submitForm();
    }

    /**
	 * 控制优惠金额和优惠券使用下限金额在表单中选择的逻辑
	 */
    function clearDiscountCouponLowAmount() {
        /**指定表单字段更新成功的验证状态**/
        updateValidateFieldStatus('discountCouponAmount');
        $("#discountCouponLowAmount").val("");
        reValidateField( 'discountCouponLowAmount');
    }

</script>

<!--添加编辑模态框（Modal） -->
<div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel"></h5>
			</div>
			<div class="modal-body">
				<form id="form1" method="post" action="${basePath}/back/discountCoupon/saveOrUpdateDiscountCoupon.sjson">
					<input id="discountCouponId" type="hidden" name="discountCouponId" />
					<input id="discountCouponCode" type="hidden" name="discountCouponCode" value=""/>
					<input id="useStatus" type="hidden" name="useStatus" value=""/>
					<input id="isPass" type="hidden" name="isPass" />
					<input id="checkTime" type="hidden" name="checkTime" value=""/>
					<input id="verifier" type="hidden" name="verifier" value=""/>
					<input id="createTime" type="hidden" name="createTime"/>
					<input id="updateTime" type="hidden"  name="updateTime"/>
					<input id="surplus" type="hidden"  name="surplus"/>
					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>优惠劵名称:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="discountCouponName" name="discountCouponName" placeholder="请输入优惠劵名称"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>优惠金额:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="discountCouponAmount" name="discountCouponAmount" onkeydown="clearDiscountCouponLowAmount()" placeholder="请输入优惠金额"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>优惠券使用下限金额:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="discountCouponLowAmount" name="discountCouponLowAmount" placeholder="请输入优惠券使用下限金额"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>发放个数:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="distributionCount" name="distributionCount" placeholder="请输入发放个数"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>开始时间:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input id="beginTime" name="beginTime" class="form-control" type="text" value="" placeholder="请选择活动开始时间" style="font-size: 12px;" readonly>
								</div>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>结束时间:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<div class="form-group">
									<input id="expirationTime" name="expirationTime" class="form-control" type="text" value="" placeholder="请选择活动结束时间" style="font-size: 12px;" readonly>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>优惠券模板:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<select id="templateType" name="templateType" class="form-control" style="width:250px;" >
									<option value="1">模板一</option>
									<option value="2">模板二</option>
									<option value="3">模板三</option>
									<option value="4">模板四</option>
								</select>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary" onclick="submitDiscountCouponForm()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
